package com.ylbh.modules.freight.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.utils.PageDTO;
import com.ylbh.common.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.Query;

import com.ylbh.modules.freight.mapper.ShoppingFreightMapper;
import com.ylbh.modules.freight.entity.ShoppingFreight;
import com.ylbh.modules.freight.service.ShoppingFreightService;


@Service("shoppingFreightService")
public class ShoppingFreightServiceImpl extends ServiceImpl<ShoppingFreightMapper, ShoppingFreight> implements ShoppingFreightService {

    @Autowired
    private ShoppingFreightMapper shoppingFreightMapper;

    /**
     * 运费模板列表
     * @param pageDTO
     * @return
     */
    @Override
    public PageUtils queryFreightList(PageDTO pageDTO) {
        Page<Integer> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        List<ShoppingFreight> list = shoppingFreightMapper.selectFreightList(pageDTO.getKeywords(), page);
        return new PageUtils(new Page<ShoppingFreight>().setRecords(list).setTotal(page.getTotal()));
    }

    /**
     * 修改运费信息
     * @param shoppingFreight
     */
    @Override
    public void modifyFreightById(ShoppingFreight shoppingFreight) {
        shoppingFreight.setUpdateTime(new Date());
        shoppingFreight.setUpdateUserId(ShiroUtils.getUserId().intValue());
        shoppingFreightMapper.updateById(shoppingFreight);
    }

    /**
     * 新增运费模板
     * @param shoppingFreight
     */
    @Override
    public void addFreight(ShoppingFreight shoppingFreight) {
        shoppingFreight.setCreateTime(new Date());
        shoppingFreight.setCreateUserId(ShiroUtils.getUserId().intValue());
        shoppingFreightMapper.insert(shoppingFreight);
    }

    /**
     * 删除运费模板
     * @param ids
     */
    @Override
    public String removeFreightById(List<Long> ids) {
        String msg = "成功";
        for (Long id:ids) {
            // 查询商品是否已经挂载这个运费模板
            int count = shoppingFreightMapper.selectIsGoodsByFreightId(id);
            if (count == 0) {
                shoppingFreightMapper.deleteFreightById(id, new Date(), ShiroUtils.getUserId());
            } else {
                msg = "无法移除以绑定到商品的模板";
            }
        }
        return msg;
    }

    /**
     * 修改启用状态
     * @param type
     * @param id
     */
    @Override
    public void modifyIsEnableById(Integer type, Integer id) {
        ShoppingFreight freight = new ShoppingFreight();
        freight.setIsEnable(type + "");
        freight.setFreightId(id.longValue());
        freight.setUpdateTime(new Date());
        freight.setUpdateUserId(ShiroUtils.getUserId().intValue());
        shoppingFreightMapper.updateById(freight);
    }

    /**
     * 获取启用状态运费模板
     * @param pageDTO
     * @return
     */
    @Override
    public PageUtils queryEnableFreightList(PageDTO pageDTO) {
        Page<Integer> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        List<ShoppingFreight> list = shoppingFreightMapper.selectEnableFreightList(page);
        return new PageUtils(new Page<ShoppingFreight>().setRecords(list).setTotal(page.getTotal()));
    }
}
